package com.blueocean_health.care.management.common.context;

import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

@Configuration
public class BeanConfig {
	public PoolingHttpClientConnectionManager poolingHttpClientConnectionManager() {
		PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager();
		poolingHttpClientConnectionManager.setMaxTotal(10);
		poolingHttpClientConnectionManager.setDefaultMaxPerRoute(5);
		return poolingHttpClientConnectionManager;
	}

	@Bean
	public HttpClientBuilder httpClientBuilder() {
		HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
		httpClientBuilder.setConnectionManager(poolingHttpClientConnectionManager());
		return httpClientBuilder;
	}

	@Bean
	public CloseableHttpClient httpClient() {
		return httpClientBuilder().build();
	}
	@Bean
	public HttpComponentsClientHttpRequestFactory clientHttpRequestFactory(){
		HttpComponentsClientHttpRequestFactory hcchrf = new HttpComponentsClientHttpRequestFactory(httpClient());
		hcchrf.setConnectTimeout(30000);
		hcchrf.setConnectTimeout(30000);
		hcchrf.setConnectionRequestTimeout(200);
		return hcchrf;
	}
	@Bean
	public RestTemplate restTemplate() {
		return new RestTemplate(clientHttpRequestFactory());
	}
}
